<?php 
	
namespace app\count\controller;
use think\Db; // 引用DB类
use think\Request;
use think\Session;

class Count
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index(){
    	
    }
    //获取账户信息列表
    public function countList($id){
    	$role		= Db::table("login")
    			   -> where("login_name",$id)
    			   -> select();
    	if($role[0]["register_role"]==2){
    		$countList	= Db::table("count")
	    			   -> where("user_name",$id)
	    			   -> order("count_id asc")
	    			   -> select();
	    	$count		= count($countList);
    	}else{
    		$countList	= Db::table("count")
	    			   -> order("count_id asc")
	    			   -> select();
	    	$count		= count($countList);
    	}
    	
    	//var_dump($shopList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$countList]);
    
    }
    
    
    //新增付款方式信息
    public function addCount(){
    	$count_type				= request()->post("count_type");
    	$count_Alipay_num		= request()->post("count_Alipay_num");
    	$count_Alipay_nickname	= request()->post("count_Alipay_nickname");
    	$count_bank_num			= request()->post("count_bank_num");
    	$count_bank_name		= request()->post("count_bank_name");
    	$user_name				= request()->post("user_name");
    	
    	if($count_type==""){
    		return json(["msg"=>"请先选择账户类型","code"=>500]);
    	}else{
    		if($count_type=="支付宝"){
    			$addMoney	= Db::table("count")
	    				   -> insert([
	    				   		"count_type"	=> $count_type,
	    				   		"count_num"		=> $count_Alipay_num,
	    				   		"count_name"	=> $count_Alipay_nickname,
	    				   		"user_name"		=> $user_name,
	    				   		"count_addTime"	=> Db::raw("now()")
	    				   ]);
    		}else{
    			$addMoney	= Db::table("count")
	    				   -> insert([
	    				   		"count_type"	=> $count_type,
	    				   		"count_num"		=> $count_bank_num,
	    				   		"count_name"	=> $count_bank_name,
	    				   		"user_name"		=> $user_name,
	    				   		"count_addTime"	=> Db::raw("now()")
	    				   ]);
    		}
    		
    		return json(["msg"=>"添加成功","code"=>200]);
    	}
    }
    
    
    //删除单个任务
    public function delCount($id){
    	$isDel	= Db::table("count")
    		   -> where("count_id",$id)
    		   -> delete();
    		   
    	if($isDel){
    		return json(["msg"=>"删除成功","code"=>200]);
    	}else{
    		return json(["msg"=>"请重试","code"=>500]);
    	}
    }
    
    //批量删除
    public function delMoreCount($count_id){
    	foreach($count_id as $count_id){
			$del	= Db::table('count')
			   	   -> where('count_id', $count_id)
			   	   -> delete();
		}
    }
    
    //搜索任务
    public function countSearch(){
    	$key			= request()->post("key");
    	$user_name		= request()->post("user_name");
    	$role		= Db::table("login")
    			   -> where("login_name",$user_name)
    			   -> select();
    	if($role[0]["register_role"]==2){
    		$isCount		= Db::table("count")
							-> where("user_name",$user_name)
							-> where("count_type|count_num|count_name","like","%".$key."%")
					   		-> order("count_addTime asc")
					   		-> select();
			$count			= count($isCount);
    	}else{
    		$isCount		= Db::table("count")
							-> where("count_type|count_num|count_name","like","%".$key."%")
					   		-> order("count_addTime asc")
					   		-> select();
			$count			= count($isCount);
    	}
    	
				   
		return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$isCount]);
    }
    
}
